 
math.randomseed(2)
-- 平均速度
--[[
总路程: 16000  个像素
总时间: 20 秒 （逻辑上划分为20段）
第1名平均速度: 800 像素/秒
后一名较前一名平均速度差：5~10
一条鱼每秒速度变化差值：50
]]
local vList = 
    {
        800, 800 - 50, 800 - 36, 800 - 20
    }
local split = 10
local between = 20
local range = 150




function genVelMatrix(vList)
	local matrix = {}
	for i = 1,#vList do
		matrix[i] = {}
		local v = vList[i]
		for j = 1,split do
			matrix[i][j] = v 
		end
	end
	return matrix
end



function changeSpeedRandom(matrix )
 	for i = 1,#matrix do
		local list = matrix[i]
		local split = #list
		for j = 1 + 1,split/2 do

			local much = math.random(- range,range)

			list[j] = list[j] + much
			list[split + 1 - j] = list[split + 1 - j] - much
		end
	end
end 

function smoothSpeedMatrix(matrix )
	local newM = {}
 	for i = 1,#matrix do
		local list = matrix[i]
		local newList = {}
		newM[i] = newList
		local split = #list
		for j = 1 , split  do
			local v1 = list[j]
			-- local v1 ,v2 = list[j-1],list[j]
			-- local unit = (v2 - v1)/3
			table.insert(newList,v1)
			table.insert(newList,v1)
			table.insert(newList,v1)
			table.insert(newList,v1)
			-- table.insert(newList,v1 + unit * 1)
			-- table.insert(newList,v1 + unit * 2)
			-- table.insert(newList,v1 + unit * 3)
		end
		print(#newList)
	end
	-- 124 /6 = 
	return newM
end  

function makeAccMatrix( )
	local am = {}
	for i = 1,4 do
		local list = {}
		am[i] = list
		for i = 1,19 do
			local a = math.random(0,between)
			table.insert(list,a)
			table.insert(list,-a)
		end
	end
	return am
end

function printTable( matrix )
	print("{")
	for i = 1,#matrix do
		local list = matrix[i]
		local str = table.concat (list ,",")
	print("  { ".. str .." },")
	end
	print("}")
end

function calAverageSpeed( matrix )
	for i = 1,#matrix do
		local list = matrix[i]
		local total = 0
		for j = 1,#list do
			total = list[j] + total
		end
		print("index".. i .." :",total/#list)
	end
end


local m = genVelMatrix(vList)
changeSpeedRandom(m)
printTable(m)
m = smoothSpeedMatrix(m)
printTable(m)


calAverageSpeed( m )

local am = makeAccMatrix( )
printTable(am)

local l = {800,800,800,800,593,593,593,593,714.70967741935,714.70967741935,714.70967741935,714.70967741935,724.70967741935,724.70967741935,724.70967741935,724.70967741935,792.70967741935,792.70967741935,792.70967741935,492.70967741935,584.70967741935,584.70967741935,584.70967741935,959.70967741935,1027.7096774194,1027.7096774194,1027.7096774194,1027.7096774194,1037.7096774194,1037.7096774194,1037.7096774194,1037.7096774194,820.70967741935,820.70967741935,820.70967741935,820.70967741935,913.70967741935,913.70967741935,913.70967741935,913.70967741935}

local total = 0
for i = 1,#l do
	total = total + l[i]
end
print(total/#l)
